package com.example.hotel.controller;

import com.example.hotel.entity.MemberType;
import com.example.hotel.entity.ResultModel;
import com.example.hotel.service.MemberTypeService;
import com.example.hotel.utils.ResultUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

import static com.example.hotel.utils.CommonUtils.*;

/**
 * (MemberType)表控制层
 */
@RestController
@RequestMapping("memberType")
public class MemberTypeController {
    /**
     * 服务对象
     */
    @Resource
    private MemberTypeService memberTypeService;

    /**
     * 调用 ResultUtils
     */
    private final ResultUtils resultUtils = new ResultUtils();

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public MemberType selectOne(Integer id) {
        return this.memberTypeService.queryById(id);
    }

    /**
     * 添加
     */
    @RequestMapping(value = "insertMemberType", method = RequestMethod.POST)
    public ResultModel insertMemberType(MemberType memberType) {
        return resultUtils.InsertResultUtils(memberType, memberTypeService.insert(memberType));
    }

    /**
     * 首先查询添加的内容是否存在
     * 若不存在则进行添加操作
     */
    @RequestMapping(value = "insertnoexistMemberType", method = RequestMethod.POST)
    public ResultModel insertnoexistMemberType(MemberType memberType) {
        ResultModel resultModel = new ResultModel();//new实体对象
        try {
            //queryByName后面指定要查询的字段
            MemberType existmemberType = memberTypeService.queryByName(memberType);
            if (existmemberType != null) {
                ResultShow(resultModel, CODETWO, EXISTEDUSER, new HashMap<>());
            } else {
                memberType = memberTypeService.insert(memberType);
                ResultShow(resultModel, CODEONE, INSERTSUCCESS, memberType);
            }
        } catch (Exception e) {
            ResultShow(resultModel, CODEMINUSONE, INSERTFAIL, new HashMap<>());
        }
        return resultModel;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "deleteMemberType", method = RequestMethod.POST)
    public ResultModel deleteMemberType(Integer id) {
        return resultUtils.DeleteResultUtils(memberTypeService.deleteById(id));
    }

    /**
     * 修改
     */
    @RequestMapping(value = "updateMemberType", method = RequestMethod.POST)
    public ResultModel updateMemberType(MemberType memberType) {
        return resultUtils.UpdateResultUtils(memberType, memberTypeService.update(memberType));
    }

    /**
     * 查询
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.POST)
    public ResultModel selectAll(MemberType memberType) {
        List<MemberType> memberTypeList = null;
        return resultUtils.SelectResultUtils(memberTypeList, memberTypeService.queryAll(memberType));
    }

    /**
     * 通过指定字段查询单条数据
     */
    @RequestMapping(value = "selectByName", method = RequestMethod.POST)
    public MemberType selectByName(MemberType memberType) {
        return this.memberTypeService.queryByName(memberType);
    }

}